package com.xoms.front.controller;

import com.xoms.front.controller.res.BaseResult;
import com.xoms.front.controller.res.ListResult;
import com.xoms.front.controller.res.ObjectResult;
import com.xoms.front.controller.res.bean.*;
import com.xoms.front.controller.springmvc.req.*;
import com.xoms.front.model.AccountBean;
import com.xoms.front.model.MemberBean;
import com.xoms.front.service.RoomService;
import com.xoms.front.service.exception.ServiceException;
import com.xoms.front.service.res.PageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: tzz(Tian)
 * Date: 2018/5/25 14:47
 */
@Component
public class RoomController {
    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    RoomService roomServiceImpl;

    public ListResult<RRoomDimensionBean> getDimensions(HttpServletRequest rquest, HttpServletResponse response) {
        ListResult<RRoomDimensionBean> result = new ListResult<RRoomDimensionBean>();
        try {
            PageResult<RRoomDimensionBean> listData = roomServiceImpl.getRoomDimensions();
            if (listData != null) {
                result.setData(listData.getData());
                result.setTotalSize(listData.getCount());
            }

        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RRoomDimensionDetailBean> getDimensionDetails(GetRoomDimensionDetailsParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RRoomDimensionDetailBean> result = new ListResult<RRoomDimensionDetailBean>();
        try {
            result = roomServiceImpl.getDimensionDetails(param);

        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RRoomStatusListBean> getRoomStatusList(GetRoomStatusListParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RRoomStatusListBean> result = new ListResult<RRoomStatusListBean>();
        try {
            result = roomServiceImpl.getRoomStatusList(param.getScales());
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RRoomListBean> getRoomList(GetRoomListParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RRoomListBean> result = new ListResult<RRoomListBean>();
        try {
            result = roomServiceImpl.getRoomList(param);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RFilmListBean> getFilmList(GetFilmListParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RFilmListBean> result = new ListResult<RFilmListBean>();
        try {
            result = roomServiceImpl.getFilmList(param);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ObjectResult<RRoomInfoBean> getRoomInfo(GetByIdParam param, HttpServletRequest request, HttpServletResponse response) {
        ObjectResult<RRoomInfoBean> result = new ObjectResult<>();
        try {
            RRoomInfoBean bean = roomServiceImpl.getRoomInfo(param);
            result.setData(bean);
            result.setFlag(bean != null);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RRoomBookInfoBean> getRoomBookList(GetByIdParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RRoomBookInfoBean> result = new ListResult<RRoomBookInfoBean>();
        try {
            result = roomServiceImpl.getRoomBookList(param);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ObjectResult<RUserInfoBean> getUserInfoByCard(GetUserInfoByCardParam param, HttpServletRequest request, HttpServletResponse response) {
        ObjectResult<RUserInfoBean> result = new ObjectResult<>();
        try {
            RUserInfoBean bean = roomServiceImpl.getUserInfoByCard(param);
            result.setData(bean);
            result.setFlag(bean != null);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ObjectResult<RUserInfoBean> getUserInfoByTel(GetUserInfoByTelParam param, HttpServletRequest request, HttpServletResponse response) {
        ObjectResult<RUserInfoBean> result = new ObjectResult<>();
        try {
            RUserInfoBean bean = roomServiceImpl.getUserInfoByTel(param);
            result.setData(bean);
            result.setFlag(bean != null);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public BaseResult roomLock(RoomLockParam param, HttpServletRequest request, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");
            result.setFlag(true);
            result.setMsg(roomServiceImpl.roomLock(account, param));
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public BaseResult roomUnLock(RoomLockParam param, HttpServletRequest request, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");
            roomServiceImpl.roomUnLock(account, param);
            result.setFlag(true);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ObjectResult<ROrderInfoBean> createOrder(CreateOrderParam param, HttpServletRequest request, HttpServletResponse response) {
        ObjectResult<ROrderInfoBean> result = new ObjectResult<>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");
            ROrderInfoBean bean = roomServiceImpl.createOrder(account, param);
            result.setData(bean);
            result.setFlag(bean != null);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ObjectResult<ROrderInfoBean> setOrderAuthPrice(EditOrderAuthPriceParam param, HttpServletRequest request, HttpServletResponse response) {
        ObjectResult<ROrderInfoBean> result = new ObjectResult<>();
        try {
            ROrderInfoBean bean = roomServiceImpl.setOrderAuthPirce(param);
            result.setData(bean);
            result.setFlag(bean != null);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public BaseResult roomOpen(RoomOpenParam param, HttpServletRequest request, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        try {
            result.setFlag(roomServiceImpl.roomOpen(param));
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ObjectResult<ROrderPayBean> orderPayWay(RoomOrderPayWayParam param, HttpServletRequest request, HttpServletResponse response) {
        ObjectResult<ROrderPayBean> result = new ObjectResult<>();
        try {
            ROrderPayBean bean = roomServiceImpl.orderPayWay(param);
            result.setData(bean);
            result.setFlag(bean != null);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ObjectResult<ROrderInfoBean> createTimeOrder(CreateTimeOrderParam param, HttpServletRequest request, HttpServletResponse response) {
        ObjectResult<ROrderInfoBean> result = new ObjectResult<>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");
            // Todo account 为空
            if (account == null) {
                account = new AccountBean();
            }
            ROrderInfoBean bean = roomServiceImpl.createTimeOrder(account, param);
            result.setData(bean);
            result.setFlag(bean != null);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ObjectResult<ROrderInfoBean> createChangeOrder(CreateChangeOrderParam param, HttpServletRequest request, HttpServletResponse response) {
        ObjectResult<ROrderInfoBean> result = new ObjectResult<>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");
            // Todo account 为空
            if (account == null) {
                account = new AccountBean();
            }
            ROrderInfoBean bean = roomServiceImpl.createChangeOrder(account, param);
            result.setData(bean);
            result.setFlag(bean != null);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }
}
